<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="../../plugins/mui/mui.min.css" rel="stylesheet"/>
		<link href="../../css/style.css" rel="stylesheet"/>
		<link href="../../css/literary/news.css" rel="stylesheet"/>
		<script src="../../js/statistics.js"></script>
	</head>
	<body>
		<header class="mui-bar mui-bar-nav">
		    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
		    <h1 class="mui-title">文体动态</h1>
		</header>
		<div class="paging">
			<ul class="clear">
				<li class="active" newsId="0">
					全部
				</li>
			</ul>
		</div>
		<div id="refreshContainer" class="mui-content mui-scroll-wrapper">
			<div class="news_body mui-scroll">
				<ul>
					
				</ul>
			</div>
		</div>
		<div class='kong'>
			<img src="../../images/my/index/null.png" alt="" />
		</div>
		<script src="../../plugins/template/template.js"></script>
		<script src="../../plugins/jquery/jquery-3.1.0.min.js"></script>
		<script src="../../plugins/mui/mui.min.js"></script>
		<script src="../../js/getApiToken.js"></script>
		<script src="../../js/fontset.js"></script>
		<script src="../../js/md5.js"></script>
		<script id="news-list" type="text/html">
			{{each data as value i}}
			<li class="clear" onclick="goNewDetail({{value.id}},'{{value.front_cover}}')">
				<div>
					<img src="{{baseUrl}}{{value.front_cover}}" /></img>
				</div>
				<div>
					<h4>{{value.title}}</h2>
					<span>{{value.views}}</span>
					<span>{{value.create_time}}</span>
				</div>
			</li>
			{{/each}}
		</script>
		<script id="type-list" type="text/html">
			{{each data as value i}}
			<li newsId="{{i}}">
				{{value}}
			</li>
			{{/each}}
		</script>
		<script>
			var first = 0,
				id = 0,
				lastId;
			mui.init({
				pullRefresh : {
					container:"#refreshContainer",//下拉刷新容器标识，querySelector能定位的css选择器均可，比如：id、.class等
					up : {
						height:50,//可选.默认50.触发上拉加载拖动距离
						auto:false,//可选,默认false.自动上拉加载一次
						contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
						contentnomore:'没有更多数据了',//可选，请求完毕若没有更多数据时显示的提醒内容；
						callback :function pullfresh() {
							if(true){
								console.log(id+'--'+lastId)
								myAjax('physical/News/getlist', 'post', {
									'type':id,
								 	'lastId':lastId,
								 	'total':6
								}, function(data) {
									if(data.code == 0){
										console.log(JSON.stringify(data))
										if(data.data.length == 0){
											mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
											
										}else{
											if(data.data.length < 6){
												mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
											}else{
												mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
											}
											loadMore(data.data);
											var length = data.data.length-1;
											lastId = data.data[length].id;
											console.log(lastId)
										}
									}else{
										mui.toast(data.error_msg);
									}
								})
							}
							first++;
		
						}
					}
				}
			});
			mui.plusReady(function(){
				console.log(8)
				myAjax('physical/News/gettype','get',{},function(data){
					if(data.code == 0){
						console.log(JSON.stringify(data.data))
						initType(data.data);
					}
						
				});
				myAjax('physical/News/getlist','get',{'total':8},function(data){
					console.log(JSON.stringify(data))
					if(data.code == 0){
						if(!data.data.length){
				 			$('.kong').show();
				 			$("#refreshContainer").hide();
							mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
				 		}else{
				 			var length = data.data.length-1;
							lastId=data.data[length].id;
							initList(data.data);
							mui('#refreshContainer').pullRefresh().enablePullupToRefresh();
				 		}
					}
				});
			})
			//类目初始化
			function initType(data){
				var list = {
					data:data
				};
				var html = template('type-list',list);
				$(".paging").find('ul').append(html);
			}
			//列表初始化
			function initList(data){
				var list = {
					data:data,
					baseUrl:_baseUrl
				};
				var html = template('news-list',list);
				$(".news_body").find('ul').empty().append(html);
			}
			//加载更多
			function loadMore(data){
				var list = {
					data:data,
					baseUrl:_baseUrl
				};
				var html = template('news-list',list);
				$(".news_body").find('ul').append(html);
			}
			//跳转详情
			function goNewDetail(id,img){
    			mui.openWindow({
    				url:'news_detail.html',
    				id:'news_detail',
    				extras:{
    					detail_id:id,
    					shareimg:img
    				},
    				waiting:{
			      		autoShow:true,//自动显示等待框，默认为true
			      		title:'正在加载...',//等待对话框上显示的提示内容
					},
					show:{
						autoShow:true,
						aniShow:'fade-in'
					}
    			});
    		}
			$(function(){
				//分页的切换
				$(".paging").delegate('li','tap',function(){
					id = $(this).attr('newsId');
					$(this).addClass('active').siblings().removeClass('active');
					mui('#refreshContainer').pullRefresh().refresh(true);
					myAjax('physical/News/getlist','post',{
					 	'type':id,
					 	'lastId':0,
					 	'total':8
					},function(data){
					 	if(data.code == 0){
					 		if(!data.data.length){
					 			$('.kong').show();
					 			$("#refreshContainer").hide();
					 			$(".news_body").find('ul').empty();
								mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
					 		}else{
					 			$('.kong').hide();
					 			$("#refreshContainer").show();
					 			var length = data.data.length-1;
								lastId=data.data[length].id;
								initList(data.data);
					 		}
					 		
					 	}
					})
				})
				
			})	
		</script>
    	
	</body>
</html>
